package com.eostoken.eos_wallet.ui.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.view.View;

import androidx.appcompat.widget.AppCompatTextView;
import androidx.cardview.widget.CardView;
import androidx.core.content.ContextCompat;

import com.blankj.utilcode.util.BarUtils;
import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.ObjectUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.eostoken.eos_wallet.R;
import com.eostoken.eos_wallet.base.BaseActivity;
import com.eostoken.eos_wallet.bean.AccountInfo;
import com.eostoken.eos_wallet.db.MgpWallet;
import com.eostoken.eos_wallet.helper.DialogHelper;
import com.eostoken.eos_wallet.utils.ClipboardUtils;
import com.fredy.mvp.BasePresenter;
import com.fredy.mvp.BaseView;
import com.fredy.mvp.Model;
import com.qmuiteam.qmui.widget.QMUITopBarLayout;
import com.qmuiteam.qmui.widget.popup.QMUIFullScreenPopup;
import com.qmuiteam.qmui.widget.roundwidget.QMUIRoundButton;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import cn.bingoogolapple.qrcode.zxing.QRCodeEncoder;

import static com.eostoken.eos_wallet.utils.Constants.EXTRA_ACCOUNT_INFO;
import static com.eostoken.eos_wallet.utils.Constants.EXTRA_WALLET;
import static com.eostoken.eos_wallet.utils.Constants.TO_IMPORT_WALLET_MNEMONIC;
import static com.eostoken.eos_wallet.utils.Constants.TO_IMPORT_WALLET_PRIVATEKEY;

public class ExportPrivateKeyOrMnemonicsActivity extends BaseActivity {
    @BindView(R.id.topbar)
    QMUITopBarLayout topbar;
    @BindView(R.id.publickeyTv1)
    AppCompatTextView publickeyTv1;
    @BindView(R.id.privatekeyTv1)
    AppCompatTextView privatekeyTv1;
    @BindView(R.id.publickeyTv2)
    AppCompatTextView publickeyTv2;
    @BindView(R.id.privatekeyTv2)
    AppCompatTextView privatekeyTv2;
    @BindView(R.id.ownerTv)
    AppCompatTextView ownerTv;
    @BindView(R.id.activeTv)
    AppCompatTextView activeTv;
    @BindView(R.id.copyTv1)
    AppCompatTextView copyTv1;
    @BindView(R.id.qrCodeBtn1)
    QMUIRoundButton qrCodeBtn1;
    @BindView(R.id.cardView1)
    CardView cardView1;
    @BindView(R.id.copyTv2)
    AppCompatTextView copyTv2;
    @BindView(R.id.qrCodeBtn2)
    QMUIRoundButton qrCodeBtn2;
    @BindView(R.id.cardView2)
    CardView cardView2;
    @BindView(R.id.accountNameTv)
    AppCompatTextView accountNameTv;
    @BindView(R.id.publickeyTv3)
    AppCompatTextView publickeyTv3;
    @BindView(R.id.mnemonicsTv)
    AppCompatTextView mnemonicsTv;
    @BindView(R.id.copyTv3)
    AppCompatTextView copyTv3;
    @BindView(R.id.qrCodeBtn3)
    QMUIRoundButton qrCodeBtn3;
    @BindView(R.id.cardView3)
    CardView cardView3;

    private String accountInfoJson;
    private AccountInfo accountInfo;
    private MgpWallet wallet;
    private String privateKey;
    private Bitmap qrCodeBitmap;
    private Map<String, Object> map = new HashMap<>();
    private String json;
    private QMUIFullScreenPopup popup;
    private int size = 200;
    private boolean isPrivateKey;
    private StringBuilder mnemonicSB;

    @Override
    public int getContentView() {
        return R.layout.activity_export_privatekey;
    }

    @Override
    protected void initData() {
        Intent intent = getIntent();
        isPrivateKey = intent.getBooleanExtra("isPrivateKey", true);
        wallet = intent.getParcelableExtra(EXTRA_WALLET);
        accountInfoJson = intent.getStringExtra(EXTRA_ACCOUNT_INFO);
        if (!ObjectUtils.isEmpty(accountInfoJson))
            accountInfo = GsonUtils.fromJson(accountInfoJson, AccountInfo.class);
        if (isPrivateKey) {
            privateKey = wallet.getPrivateKey();
        } else {
            mnemonicSB = new StringBuilder();
            for (String s : wallet.getMnemonicCode()) {
                mnemonicSB.append(s).append(" ");
            }
        }
        map.put("type", isPrivateKey ? TO_IMPORT_WALLET_PRIVATEKEY : TO_IMPORT_WALLET_MNEMONIC);
        map.put(isPrivateKey ? "privateKey" : "mnemonic", isPrivateKey ? privateKey : mnemonicSB.toString());
        json = GsonUtils.toJson(map);

        qrCodeBitmap = QRCodeEncoder.syncEncodeQRCode(json, size);
    }

    @Override
    public void initView() {
        BarUtils.setStatusBarColor(this, ContextCompat.getColor(this, R.color.app_color_blue));
        BarUtils.setStatusBarLightMode(this, false);
        topbar.setTitle(isPrivateKey ? StringUtils.getString(R.string.str_export_privateKey) : StringUtils.getString(R.string.str_export_mnemonics));
        topbar.addLeftBackImageButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        cardView1.setVisibility(isPrivateKey ? View.VISIBLE : View.GONE);
        cardView2.setVisibility(isPrivateKey ? View.VISIBLE : View.GONE);
        cardView3.setVisibility(isPrivateKey ? View.GONE : View.VISIBLE);
        if (isPrivateKey) {
            if (!ObjectUtils.isEmpty(accountInfo)) {
                List<AccountInfo.PermissionsBean> permissionsBeanList = accountInfo.getPermissions();
                AccountInfo.PermissionsBean activePermissionsBean = permissionsBeanList.get(0);
                AccountInfo.PermissionsBean ownerPermissionsBean = permissionsBeanList.get(1);

                String activeName = activePermissionsBean.getPerm_name();
                String ownerName = ownerPermissionsBean.getPerm_name();

                AccountInfo.PermissionsBean.RequiredAuthBean activeRequiredAuthBean = activePermissionsBean.getRequired_auth();
                AccountInfo.PermissionsBean.RequiredAuthBean ownerRequiredAuthBean = ownerPermissionsBean.getRequired_auth();

                List<AccountInfo.PermissionsBean.RequiredAuthBean.KeysBean> activeKeysBeanList = activeRequiredAuthBean.getKeys();
                List<AccountInfo.PermissionsBean.RequiredAuthBean.KeysBean> ownerKeysBeanList = ownerRequiredAuthBean.getKeys();

                AccountInfo.PermissionsBean.RequiredAuthBean.KeysBean activeKeysBean = activeKeysBeanList.get(0);
                AccountInfo.PermissionsBean.RequiredAuthBean.KeysBean ownerKeyBean = ownerKeysBeanList.get(0);

                String activeKey = activeKeysBean.getKey();
                String ownerKey = ownerKeyBean.getKey();

                ownerTv.setText(ownerName);
                publickeyTv1.setText(ownerKey);
                activeTv.setText(activeName);
                publickeyTv2.setText(activeKey);
            }
            privatekeyTv1.setText(privateKey);
            privatekeyTv2.setText(privateKey);

        } else {
            accountNameTv.setText(wallet.getCurrentAccountName());
            publickeyTv3.setText(wallet.getPublicKey());
            mnemonicsTv.setText(mnemonicSB.toString());
        }
    }

    @Override
    public Model createModel() {
        return null;
    }

    @Override
    public BaseView createView() {
        return null;
    }

    @Override
    public BasePresenter createPresenter() {
        return null;
    }

    @Override
    public void finishRefresh() {

    }

    @OnClick({R.id.copyTv1, R.id.qrCodeBtn1, R.id.copyTv2, R.id.qrCodeBtn2, R.id.copyTv3, R.id.qrCodeBtn3})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.copyTv1:
            case R.id.copyTv2:
            case R.id.copyTv3:
                ClipboardUtils.copyText(isPrivateKey ? privateKey : mnemonicSB.toString());
                ToastUtils.showShort(StringUtils.getString(R.string.str_copy_success));
                break;
            case R.id.qrCodeBtn1:
            case R.id.qrCodeBtn2:
            case R.id.qrCodeBtn3:
                if (popup == null) {
                    popup = DialogHelper.showImage(ExportPrivateKeyOrMnemonicsActivity.this, qrCodeBitmap, size);
                }
                popup.show(view);
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (qrCodeBitmap != null) {
            qrCodeBitmap.recycle();
            qrCodeBitmap = null;
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }

    @Override
    public boolean isShowState() {
        return true;
    }
}
